﻿using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Presentation.Events;
using StockholmsStad.Windows.UI.Modules.Couple.Interfaces;
using StockholmsStad.Windows.UI.Modules.Couple.Models;
using Microsoft.Practices.Composite.Events;
using StockholmsStad.Windows.UI.Modules.Couple.Events;

namespace StockholmsStad.Windows.UI.Modules.Couple.Views
{
    /// <summary>
    /// Interaction logic for TabItemCoupleView.xaml
    /// </summary>
    public partial class TabItemCoupleView : ICoupleDetailsView
    {
        private int loadedTicketId;
        private readonly ICoupleService coupleService;
        private readonly SubscriptionToken subscriptionToken;

        public TabItemCoupleView(ICoupleService service, IEventAggregator eventAggregator)
        {
            InitializeComponent();
            coupleService = service;

            Loaded += TabItemCoupleView_Loaded;

            var addedCoupleEvent = eventAggregator.GetEvent<CoupleAddedEvent>();

            if (subscriptionToken != null)
                addedCoupleEvent.Unsubscribe(subscriptionToken);

            subscriptionToken = addedCoupleEvent.Subscribe(CoupleAddedEventHandler, ThreadOption.UIThread, false);
        }

        private void CoupleAddedEventHandler(CoupleDetailPresentationModel presentationModel)
        {
            DataContext = presentationModel;
        }

        private void TabItemCoupleView_Loaded(object sender, RoutedEventArgs e)
        {
            if(loadedTicketId != SelectedTicketId || loadedTicketId > 0)
            {
                Model = coupleService.GetCouple(selectedTicketId);
                loadedTicketId = selectedTicketId;
            }
            else
            {
                Model = coupleService.InitializeNewCouple();
            }
            Model.IsInChangeMode = false;
        }

        #region ICoupleDetailsView Members

        public CoupleDetailPresentationModel Model
        {
            get { return DataContext as CoupleDetailPresentationModel; }
            set { DataContext = value; }
        }

        private int selectedTicketId;
        public int SelectedTicketId
        {
            get { return selectedTicketId; }
            set
            {
                selectedTicketId = value;
                if (IsLoaded && loadedTicketId != SelectedTicketId)
                {
                    Model = coupleService.GetCouple(selectedTicketId);
                    Model.IsInChangeMode = false;
                    loadedTicketId = selectedTicketId;
                }
            }
        }

        #endregion

        private void Grid_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource.GetType() == typeof(Button))
            {
                var button = (Button)e.OriginalSource;

                if (button.Name == "CancelButton")
                {
                    if (Model == null)
                        return;

                    Model = Model.TicketId.HasValue ? coupleService.GetCouple(Model.TicketId.Value) : coupleService.InitializeNewCouple();
                }
            }

            e.Handled = true;
        }
    }
}
